package net.form.processing;

public class ElasticTransformationUtil {
	
	private double C1,C2,C3,C4,C5,C6,C7,C8;
	
	public ElasticTransformationUtil(int xA, int yA, int xB, int yB, int xC,
			int yC, int xD, int yD, int xDestA, int yDestA, int xDestB,
			int yDestB, int xDestC, int yDestC, int xDestD, int yDestD) {
		super();
		
		double w1 = ((yA - yB)*(xA - xC) / (xA - xB)) - (yA - yC);
		double w2 = ( ((xA*yA - xB*yB)*(xA - xD) ) / (xA - xB) ) - (xA*yA - xD*yD);
		double w3 = xDestC - xDestA + ((xDestA - xDestB)*(xA - xC) / (xA - xB));
		double w4 = xDestA - ( (xDestA - xDestB)*(xA - xD) / (xA - xB) );
		double w5 = ((xA*yA - xB*yB)*(xA - xC) / (xA - xB)) - (xA*yA - xC*yC);
		double w6 = ((yA - yB)*(xA - xD) / (xA - xB)) - (yA - yD);
		double w7 = yDestC - yDestA + ((yDestA - yDestB)*(xA - xC) / (xA - xB));
		double w8 = yDestA - ( (yDestA - yDestB)*(xA - xD) / (xA - xB) );
		
		
		C4 = (xDestD - w4 - (w6*w3/w1)) / (w2 - ((w5*w6) / w1) );
		C3 = (xDestC - xDestA + ((xDestA - xDestB)*(xA - xC) / (xA - xB)) - C4*w5) / w1;
		C2 = (xDestA - xDestB - C3*(yA - yB) - C4*(xA*yA - xB*yB)) / (xA - xB);
		C1 = xDestA - C2*xA - C3*yA - C4*xA*yA;
		C8 = (yDestD - w8 - (w6*w7/w1)) / ( w2 - ((w5*w6) / w1));
		C7 = (yDestC - yDestA + ((yDestA - yDestB)*(xA - xC) / (xA - xB)) - C8*w5) / w1;
		C6 = (yDestA - yDestB - C7*(yA - yB) - C8*(xA*yA - xB*yB)) / (xA - xB);
		C5 = yDestA - C6*xA - C7*yA - C8*xA*yA;
		
	}

	public int r(int xA, int yA) {
		return (int) (C1 + C2 * xA + C3 * yA + C4 * xA * yA);
	}

	public int s(int xA, int yA) {
		return (int) (C5 + C6 * xA + C7 * yA + C8 * xA * yA);
	}

}
